from django.shortcuts import render
from rest_framework.generics import ListAPIView,RetrieveAPIView
from course.models import CourseDirectionModel,CourseCategoryModel,CourseModel,CourseChapterModel
from course.seriali import CourseCategoryModelSerializer,CourseDirectionModelSerializer,CouserSerializer,CourseRetrieveSerializer,CourseChapterSerializer
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter
# Create your views here.
class CourseDirectionView(ListAPIView):
    queryset = CourseDirectionModel.objects.filter(is_show=True,is_deleted=False).order_by('-id')
    serializer_class = CourseDirectionModelSerializer

# 课程列表分页
class CoursePageNumberPagination(PageNumberPagination):
    page_size = 5
    page_query_param = 'page'
    page_size_query_param = 'size'
    max_page_size = 15

class CourseCategoryionView(ListAPIView):
    serializer_class =CourseCategoryModelSerializer
    def get_queryset(self):
        directionID=int(self.kwargs.get('directionID',0))
        query_set=CourseCategoryModel.objects.filter(is_show=True,is_deleted=False).order_by('-id')
        if directionID > 0:
            query_set=query_set.filter(direction=directionID)
        return query_set

class  CourseListView(ListAPIView):
    serializer_class =CouserSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ['id','students','price']
    pagination_class = CoursePageNumberPagination
    def get_queryset(self):

        # 关键字参数
        directionID = int(self.kwargs.get("directionID",0))
        categoryID = int(self.kwargs.get("categoryID",0))
        # 重写方法拿到queryset结果
        queryset=CourseModel.objects.filter(is_show=True,is_deleted=False).order_by('id')
        if directionID > 0:
            queryset = queryset.filter(direction=directionID)
        if categoryID > 0:
            queryset= queryset.filter(category=categoryID)
        return queryset
class CourseDetaileView(RetrieveAPIView):
    # course = CourseModel.objects.get(id=1)
    # course.level   :  1
    # course.get_level_display()  : 中级
    queryset = CourseModel.objects.all()
    serializer_class = CourseRetrieveSerializer
    lookup_field = 'pk'
    lookup_url_kwarg = "courseID"
class CourseChapterListApiView(ListAPIView):
    queryset = CourseChapterModel.objects.all()
    serializer_class = CourseChapterSerializer
    lookup_field = 'pk'
    lookup_url_kwarg = 'courseID'
